package zhh.tangbao.websocket.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhh.tangbao.auxiliary.service.SystemMessageService;
import zhh.tangbao.websocket.WebSocketServiceType;
import zhh.tangbao.websocket.client.WebSocketClient;
import zhh.tangbao.websocket.pojo.WebSocketMessageEntity;
import zhh.tangbao.websocket.service.WebSocketService;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *     给客户端发送消息的实现类
 * </p>
 * @author 粥灰灰
 * <p>2023/4/20</p>
 */

@Service(WebSocketServiceType.MESSAGE)
public class MessageService implements WebSocketService {

    private Map<String, WebSocketClient> server = new ConcurrentHashMap<>();

    @Autowired
    private SystemMessageService messageService;

    @Override
    public void addClient(String id, WebSocketClient client) {
        server.put(id, client);
    }

    @Override
    public void removeClient(String id) {
        if (CollUtil.isNotEmpty(server)) {
            server.remove(id);
        }
    }

    @Override
    public void sendMessageToOne(String recordUserId, String id, WebSocketMessageEntity message) throws IOException {
        WebSocketClient client = server.get(recordUserId);
        Assert.notNull(client, "该客户端已断开连接");
        if (client.isOpen()) {
            client.sendMessage(message.toJsonString());
        }
        messageService.insert(recordUserId, id, message.getTitle(), JSONObject.toJSONString(message.getContent()));
    }

    @Override
    public void sendMessageToMany(String sendUserId, List<String> receiverIds, WebSocketMessageEntity message) throws IOException {
        if (CollUtil.isEmpty(receiverIds)) {
            return;
        }
        for (String id : receiverIds) {
            WebSocketClient client = server.get(id);
            if (null != client && client.isOpen()) {
                client.sendMessage(message.toJsonString());
            }
        }
        messageService.batchInsert(sendUserId, receiverIds, message.getTitle(), JSONObject.toJSONString(message.getContent()));
    }

    @Override
    public void sendMessageToAll(String sendUserId, WebSocketMessageEntity message) {
        try {
            // 给所有在线的人发送提醒
            Set<Map.Entry<String, WebSocketClient>> entries = server.entrySet();
            for (Map.Entry<String, WebSocketClient> e : entries) {
                WebSocketClient client = e.getValue();
                if (null != client && client.isOpen()) {
                    client.sendMessage(message.toJsonString());
                }
            }
            messageService.insertAll(sendUserId, message.getTitle(), String.valueOf(message.getContent()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
